package com.zcsy.manage.service.quartz.impl;

import java.util.Timer;
import java.util.TimerTask;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zcsy.commons.constants.Constants;
import com.zcsy.manage.model.mall.OrderInfo;
import com.zcsy.manage.persistence.weixin.StringUtil;
import com.zcsy.manage.service.mall.IOrderInfoService;
import com.zcsy.manage.service.push.IPushService;
import com.zcsy.manage.service.quartz.ITimerTaskService;

import jxl.common.Logger;

@Service
public class TimerTaskServiceImpl implements ITimerTaskService{

    private Logger logger = Logger.getLogger(TimerTaskServiceImpl.class);
    
    public static final int TASK_NO_PAY     = 1; // 未付款
    public static final int TASK_NO_CONFIRM = 2; // 未确认
    public static final int TASK_NO_FINISH  = 3; // 未收货
    public static final int TASK_UNSEND  = 4; // 待配送
    
    public static final long DELAY_NO_PAY     = 1000 * 60 * 10;
    public static final long DELAY_NO_CONFIRM = 1000 * 60 * 60 * 24;
    public static final long DELAY_NO_FINISH  = 1000 * 60 * 60 * 24;
    public static final long DELAY_UNSEND  = 1000 * 60 * 5;
    
    @Autowired 
    private IPushService pushService;
    
    @Autowired 
    private IOrderInfoService orderInfoService;
    
    @Override
    public void orderTimeTask(int type, String id) {
        Timer timer = new Timer();
        OrderTimerTask orderTimerTask = new OrderTimerTask(type, id, timer);
        timer.schedule(orderTimerTask, orderTimerTask.getDelay());
    }
    
    private class OrderTimerTask extends TimerTask{
        /**
         * the delay time, unit: ms
         */
        private long delay;
        private int type;
        private Timer timer;
        private String id;
        
        /**
         * constructor
         * @param type task type
         * @param timer the timer task
         */
        public OrderTimerTask(int type, String id, Timer timer) {
            super();
            switch (type) {
                case TASK_NO_PAY:
                    this.delay = DELAY_NO_PAY;
                    break;
                case TASK_NO_CONFIRM:
                    this.delay = DELAY_NO_CONFIRM;
                    break;
                case TASK_NO_FINISH:
                    this.delay = DELAY_NO_FINISH;
                    break;
                case TASK_UNSEND:
                    this.delay = DELAY_UNSEND;
                    break;
                default:
                    break;
            }
            this.type = type;
            this.timer = timer;
            this.id = id;
        }

        @Override
        public void run() {
            try {
                switch (type) {
                case TASK_NO_PAY:
                    logger.info("no pay task start..." + orderInfoService);
                    OrderInfo orderInfo = orderInfoService.selectByPrimaryId(id);
                    if(null != orderInfo && orderInfo.getState() == Constants.ORDER_UNPAID){
                        orderInfoService.updateOrderStateByMap(id, Constants.ORDER_CANCEL, orderInfo.getUserAccount());
                    }
                    break;
                case TASK_NO_CONFIRM:
                    logger.info("no confirm task start...");
                    break;
                case TASK_NO_FINISH:
                    logger.info("no finish task start...");
                    break;
                case TASK_UNSEND:
                    logger.info("no send task start...");
                    if(StringUtil.isEmptyString(orderInfoService.selectByPrimaryId(id).getSenderAccount())){
                        pushService.pushDistributionMsg(id,Constants.PUSH_TYPE_DISTRIBUTION);
                    }
                    break;
                default:
                    break;
            }
            } catch (Exception e) {
                e.printStackTrace();
            }
            this.timer.cancel();
        }

        public long getDelay() {
            return delay;
        }
    }
}
